package main.java.com.cooljie.controller;

import com.cooljie.entity.StudentCourse;
import com.cooljie.service.StudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by ACER on 2017/6/13.
 */
@Controller
@RequestMapping("/sc")
public class StudentCourseController {
    @Autowired
    private StudentCourseService studentCourseService;

    @RequestMapping(value = "/scList",method = RequestMethod.GET)
    public String studentCourse(Model model,HttpServletRequest request){
        int studentId= Integer.parseInt(request.getParameter("id"));
        List<StudentCourse> studentCourseList= studentCourseService.studentCourseList(studentId);
            model.addAttribute(studentCourseList);
        return "sc/list";
    }
@RequestMapping(value = "/update",method = RequestMethod.GET)
    public String alterGradeGet(HttpServletRequest request,Model model){
StudentCourse studentCourse=studentCourseService.updateGrade(Integer.parseInt(request.getParameter("id")));
    model.addAttribute(studentCourse);

        return "sc/grade";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String alterGrade(HttpServletRequest request,Model model){
        StudentCourse studentCourse=studentCourseService.selectById(Integer.parseInt(request.getParameter("scId")));
        studentCourse.setGrade(Double.parseDouble(request.getParameter("grade")));
        studentCourseService.updateGrade(studentCourse);
        model.addAttribute(studentCourse);
        return "redirect:/sc/scList?id="+studentCourse.getStudentId();
    }
}
